home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / source.exe / POSIX / SH / STD / STDC / STRNCMP.C < prev    next >
C/C++ Source or Header  |  1992-07-13  |  778b  |  41 lines

  1. #include <string.h>
  2.  
  3. /*
  4.  * strncmp - compare at most n characters of string s1 to s2
  5.  */
  6.  
  7. int                /* <0 for <, 0 for ==, >0 for > */
  8. strncmp(s1, s2, n)
  9. Const char *s1;
  10. Const char *s2;
  11. size_t n;
  12. {
  13.     register Const char *scan1;
  14.     register Const char *scan2;
  15.     register size_t count;
  16.  
  17.     scan1 = s1;
  18.     scan2 = s2;
  19.     count = n;
  20.     while (--count >= 0 && *scan1 != '\0' && *scan1 == *scan2) {
  21.         scan1++;
  22.         scan2++;
  23.     }
  24.     if (count < 0)
  25.         return(0);
  26.  
  27.     /*
  28.      * The following case analysis is necessary so that characters
  29.      * which look negative collate low against normal characters but
  30.      * high against the end-of-string NUL.
  31.      */
  32.     if (*scan1 == '\0' && *scan2 == '\0')
  33.         return(0);
  34.     else if (*scan1 == '\0')
  35.         return(-1);
  36.     else if (*scan2 == '\0')
  37.         return(1);
  38.     else
  39.         return(*scan1 - *scan2);
  40. }
  41.